﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComTools
{
    public partial class frmFanWei : Form
    {
        public frmFanWei()
        {
            InitializeComponent();
        }

        private void frmFanWei_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var url = textBox1.Text.Trim();
            if (url.EndsWith("/"))
            {
                url = url.Remove(url.Length - 1,1);
            }
            url = url + txtApiPath.Text.Trim();
            HttpWebRequest request = WebRequest.CreateHttp(url);
            request.Accept = "*/*";
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;";
            request.CookieContainer = new CookieContainer();
            var content = textBox2.Text.Trim();
            //
            if (!string.IsNullOrWhiteSpace(content))
            {
                byte[] requestContent = System.Text.Encoding.UTF8.GetBytes(content);
                System.IO.Stream rqstream = request.GetRequestStream();
                rqstream.Write(requestContent, 0, requestContent.Length);
                rqstream.Close();
            }
            //把cookies转换成字符串
            string cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
            textBox4.Text = cookiesstr;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                LoginCookie = response.Cookies;
                textBox4.Text += Environment.NewLine + response.Headers["Set-Cookie"];
                using(System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    var returnStr = reader.ReadToEnd();
                    reader.Close();
                    //
                    textBox3.Text = returnStr;
                }
                response.Close();
            }
        }

        private CookieCollection LoginCookie;

        private void btnDoRequest_Click(object sender, EventArgs e)
        {
            var url = textBox1.Text.Trim();
            if (url.EndsWith("/"))
            {
                url = url.Remove(url.Length - 1, 1);
            }
            url = url + textBox8.Text.Trim();
            HttpWebRequest request = WebRequest.CreateHttp(url);
            request.Method = "post";
            request.ContentType = "application/x-www-form-urlencoded";
            if (LoginCookie != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(LoginCookie);
            }
            //
            var content = textBox5.Text.Trim();
            if (!string.IsNullOrEmpty(content))
            {
                //StringBuilder data = new StringBuilder();
                //foreach (var kp in content.Keys)
                //{
                //    if (data.Length > 0)
                //    {
                //        data.Append("=");
                //    }
                //    data.Append($"{kp}={content[kp]}");
                //}
                byte[] requestContent = System.Text.Encoding.UTF8.GetBytes(content);
                using (Stream rqstream = request.GetRequestStream())
                {
                    rqstream.Write(requestContent, 0, requestContent.Length);
                    rqstream.Close();
                }
            }
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                textBox7.Text = response.Headers["Set-Cookie"];
                using (StreamReader rsstream = new StreamReader(response.GetResponseStream()))
                {
                    var r = rsstream.ReadToEnd();
                    rsstream.Close();
                    textBox4.Text = r;
                }
            }
        }
    }


}
